//
// Created by v on 2025/11/18.
//

#include "CMRecordService.h"

#include "config/CMConfigCpp.h"
#include "utils/rt_print.h"

class CMRecordConfig : public CMConfigCpp
{
public:
    CMRecordConfig(const char *file): CMConfigCpp(file){};
    virtual ~CMRecordConfig() {};

    std::string dev_path()
    {
        return getString("dev_path");
    }
    std::string save_path()
    {
        return getString("save_path");
    }
    size_t free_limit()
    {
        return getInt64("storage_free_limit");
    }
    uint64_t time_per_file()
    {
        return getInt64("time_per_file");
    }
    uint64_t size_per_file()
    {
        return getInt64("size_per_file");
    }
    std::string ext()
    {
        return getString("ext");
    }
};

CMRecordService::CMRecordService() :CMRecordServiceInterface("recordService")
{
    pri_config = new CMRecordConfig("./record_service.json");
    std::string dev = pri_config->dev_path();
    pri_manager = new CMStorageManager(dev);
    pri_record = new CMStorageRecord(pri_manager);
    pri_record->saveStrategy(1);
    pri_record->savePath(pri_config->save_path().c_str());
    pri_record->storageLimitAsFree(pri_config->free_limit());
    pri_record->timePerFile(pri_config->time_per_file());
    pri_record->sizePerFile(pri_config->size_per_file());
    pri_record->ext_name(pri_config->ext().c_str());

    rtinfo("[%s] : store [%llu, %llu, %llu]\n", dev.c_str(), pri_manager->all_size(),
        pri_manager->free_size(), pri_manager->available_size());
}
CMRecordService::~CMRecordService()
{
    if (pri_record)
    {
        delete pri_record;
    }
    if (pri_manager)
    {
        delete pri_manager;
    }
}

int CMRecordService::init()
{
    int ret = pri_record->init();
    return ret;
}
int CMRecordService::deinit()
{
    int ret = pri_record->deinit();
    return ret;
}
int CMRecordService::pushData(unsigned char *data, size_t size)
{
    int ret = 0;
    if (pri_record)
    {
        ret = pri_record->pushData(data, size, pri_config->ext().c_str());
    }
    return ret;
}
int CMRecordService::ctl(int type, void *args)
{
    return 0;
}
int CMRecordService::run()
{
    rtdebug("run done\n");
    return 0;
}
